package com.losl.system.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.losl.common.core.domain.entity.SysUser;
import com.losl.common.util.StringUtils;
import com.losl.system.mapper.SysUserMapper;
import com.losl.system.service.SysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Primary;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * <p>
 * 用户表 服务实现类
 * </p>
 *
 * @author maxwell
 * @since 2021-05-13
 */
@Primary
@Service
public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUser> implements SysUserService {

  @Autowired
  private PasswordEncoder passwordEncoder;

    /**
     * 通过用户名查找用户
     *
     * @param username
     * @return
     */
    @Override
    public SysUser selectUserByName(String username) {
        return baseMapper.selectUserByName(username);
    }

    /**
     * 注册用户
     * @param user
     */
    @Override
    public boolean register(SysUser user) {
        String password = passwordEncoder.encode(user.getPassword());
        user.setPassword(password);
        user.setRole(1);
        SysUser sysUser = selectUserByName(user.getUsername());
        if (StringUtils.isNull(sysUser)) {
            return this.save(user);
        }
        return false;
    }
}
